package com.ruoyi.system.service.impl;

/**
 * @Package: com.ruoyi.system.service.impl
 * @ClassName: DashboardServiceImpl.java
 * @Description:
 * @author: zhengxiaolong
 * @date: 2024/12/20 10:47
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.IComplaintService;
import com.ruoyi.system.service.IDashboardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页数据服务实现
 */
@Service
public class DashboardServiceImpl implements IDashboardService {
    @Autowired
    private ResidentMapper residentMapper;

    @Autowired
    private BuildingMapper buildingMapper;

    @Autowired
    private ParkingSpaceMapper parkingSpaceMapper;

    @Autowired
    private VisitorMapper visitorMapper;

    @Autowired
    private ComplaintMapper complaintMapper;

    /**
     * 获取统计数据
     */
    @Override
    public Map<String, Object> getStatistics() {
        Map<String, Object> data = new HashMap<>();

        LambdaQueryWrapper<Resident> residentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        residentLambdaQueryWrapper.eq(Resident::getIsDelete, 0);

        // 获取居民总数
        data.put("residentCount", residentMapper.selectCount(residentLambdaQueryWrapper));

        LambdaQueryWrapper<Building> buildLambdaQueryWrapper = new LambdaQueryWrapper<>();
        buildLambdaQueryWrapper.eq(Building::getIsDelete, 0);
        // 获取楼宇总数
        data.put("buildingCount", buildingMapper.selectCount(buildLambdaQueryWrapper));

        // 计算车位使用率
        LambdaQueryWrapper<ParkingSpace> parkingSpaceLambdaQueryWrapper = new LambdaQueryWrapper<>();
        parkingSpaceLambdaQueryWrapper.eq(ParkingSpace::getIsDelete, 0);

        int totalParkingSpaces = parkingSpaceMapper.selectCount(parkingSpaceLambdaQueryWrapper);
        int usedParkingSpaces = parkingSpaceMapper.selectUsedCount();
        double parkingRate = totalParkingSpaces == 0 ? 0 :
                (double) usedParkingSpaces / totalParkingSpaces * 100;
        data.put("parkingRate", String.format("%.1f", parkingRate));

        // 获取今日访客数
        LambdaQueryWrapper<Visitor> visitorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        visitorLambdaQueryWrapper.ge(Visitor::getVisitTime, new Date());
        data.put("todayVisitorCount", visitorMapper.selectCount(visitorLambdaQueryWrapper));

        return data;
    }

    @Override
    public List<Map<String, Object>> getLatestComplaints() {
        List<Complaint> complaints = complaintMapper.selectList(
                new LambdaQueryWrapper<Complaint>()
                        .eq(Complaint::getIsDelete, 0)
                        .orderByDesc(Complaint::getCreateTime)
                        .last("LIMIT 10")
        );

        return complaints.stream()
                .map(c -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", c.getId());
                    map.put("type", c.getHandleStatusId());
                    map.put("content", c.getDescription());
                    map.put("createTime", c.getCreateTime());
                    map.put("residentName", residentMapper.selectById(c.getResidentId()).getName());
                    return map;
                })
                .collect(java.util.stream.Collectors.toList());
    }

    @Override
    public List<Map<String, Object>> getLatestVisitors() {
        List<Visitor> visitors = visitorMapper.selectList(
                new LambdaQueryWrapper<Visitor>()
                        .eq(Visitor::getIsDelete, 0)
                        .orderByDesc(Visitor::getVisitTime)
                        .last("LIMIT 10")
        );

        return visitors.stream()
                .map(v -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", v.getId());
                    map.put("visitorName", v.getName());
                    map.put("visitTime", v.getVisitTime());
                    map.put("expectedLeaveTime", v.getLeaveTime());
                    map.put("residentName", residentMapper.selectById(v.getResidentId()).getName());
                    return map;
                })
                .collect(java.util.stream.Collectors.toList());
    }
}